<?php
class OrderModel extends Models
{
    protected $tableName='order';
    private $total, $id;

    public function getNewUID()
    {
        $beforeNum='';
        $afterNum='';

        for($i=0; $i<=2; $i++){
            $beforeNum.=rand(0, 9);
            $afterNum.=rand(0, 9);
        }

        return $beforeNum.time().$afterNum;
    }

    public function getStatus($id,$attributeName='')
    {
        if($order=$this->findModelAttributesByPk($id,array('order_status_id'))){
            if($status=$this->model->load('orderStatusModel')->findByAttributes(array('order_status_id'=>$order['order_status_id']))){
                return $attributeName ? $status[$attributeName] : $status;
            }
        }
        return false;
    }

    public function getTotalLeftPercent($id,$round=4)
    {
        $paid=0;
        if($this->total>0){
            $result=$this->total;
        }else{
            $result=$this->getTotal($id);
        }

        if($products=$this->model->load('orderProductPartialModel')->findAllByAttributes(array('order_id'=>$id))){
            foreach($products as $prod){
                $paid+=$prod['money'];
            }
        }
        if($order=$this->findModelAttributesByPk($id,array('partial_paid','total'))){
            $paid+=$order['partial_paid'];
        }

        if($paid) return round(($paid/$result)*100,$round);
        else return false;
    }

    public function getTotalLeft($id)
    {
        $paid=0;
        if($this->total>0){
            $result=$this->total;
        }else{
            $result=$this->getTotal($id);
        }

        if($products=$this->model->load('orderProductPartialModel')->findAllByAttributes(array('order_id'=>$id))){
            foreach($products as $prod){
                $paid+=$prod['money'];
            }
        }
        if($order=$this->findModelAttributesByPk($id,array('partial_paid','total'))){
            $paid+=$order['partial_paid'];
        }

        return $result-$paid;
    }

    public function getTotal($id)
    {
        if($this->total>0 && $this->id && $this->id==$id) return $this->total;
        $result=0;
        $this->id=$id;

        if($products=$this->model->load('orderProductModel')->findAllByAttributes(array('order_id'=>$id))){
            foreach($products as $prod){
                $result+=$prod['quantity']*$prod['price'];
            }
        }

        if($prodTotal=$this->model->load('orderTotalModel')->findAllByAttributes(array('order_id'=>$id))){
            foreach($prodTotal as $total){
                if($total['code']!='sub_total' && $total['code']!='total') $result+=$total['value'];
            }
        }

        $order=$this->findModelAttributesByPk($id,array('total'));

        if($result && $result!=$order['total']){
            $this->update($id,array('total'=>$result));
        }

        $this->total=$result;

        return $result;
    }

    public function getOrderProducts($id)
    {
        return $this->findAllByAttributes(array('order_id'=>$id),'order_product');
    }

    public function getOrderPartialProducts($id)
    {
        return $this->findAllByAttributes(array('order_id'=>$id),'order_product_partial');
    }

    public function getAllProducts($id)
    {
        if(!$orderProd=$this->findAllByAttributes(array('order_id'=>$id),'order_product')) return false;
        $partialProd=$this->findAllByAttributes(array('order_id'=>$id),'order_product_partial');

        return array(
            'products'=>$orderProd,
            'partial'=>$partialProd,
        );
    }

    public function getTimeLimit($id)
    {
        $limit=$this->findModelAttributesByPk($id,array('time_limit'));
        if($limit && isset($limit['time_limit'])) return $limit['time_limit'];
        else return false;
    }

    public function getTableName(){
        return $this->tableName;
    }
}